<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class SharePoster extends Base {
    public  $order_status;
    /*
     * 初始化操作
     */
    public function _initialize() {
        parent::_initialize();
        C('TOKEN_ON',false); // 关闭表单令牌验证
    }

    public function index(){
        $domain_url = getCurUrl();
        $platform = config('platform');
        $type = input('param.type') ? input('param.type') : 'wx';
        $posterset = Db::name('admin_set_poster')->where('type','product')->where('platform',$type)->order('id')->find();
        if(!$posterset){
            $posterset = [];
            $posterset['type'] = 'product';
            $posterset['platform'] = $type;
            $posterset['createtime'] = time();
            $posterset['content'] = jsonEncode([
                'poster_bg' => $domain_url.'/static/imgsrc/posterbg.jpg',
                'poster_data' => [
                    ['left' => '30px','top' => '70px','type' => 'pro_img','width' => '285px','height' => '285px','size' => '','color' => '','content' => ''],
                    ['left' => '30px','top' => '370px','type' => 'textarea','width' => '286px','height' => '47px','size' => '16px','color' => '#000','content' => '[商品名称]'],
                    ['left' => '34px','top' => '452px','type' => 'head','width' => '47px','height' => '47px','radius' => '100'],
                    ['left' => '89px','top' => '459px','type' => 'text','width' => '50px','height' => '18px','size' => '16px','color' => '#333333','content' => '[昵称]'],
                    ['left' => '90px','top' => '484px','type' => 'text','width' => '98px','height' => '14px','size' => '12px','color' => '#B6B6B6','content' => '推荐给你一个宝贝'],
                    ['left' => '35px','top' => '516px','type' => 'text','width' => '142px','height' => '22px','size' => '20px','color' => '#FD0000','content' => '￥[商品销售价]'],
                    ['left' => '125px','top' => '518px','type' => 'text','width' => '135px','height' => '16px','size' => '14px','color' => '#BBBBBB','content' => '原价:￥[商品市场价]'],
                    ['left' => '221px','top' => '446px','type' => 'qrmp','width' => '94px','height' => '94px','size' => '','color' => '','content' => ''],
                ]
            ]);
            $posterset['guize'] = "";
            $id = Db::name('admin_set_poster')->insertGetId($posterset);
        }else{
            $id = $posterset['id'];
        }
        $posterdata = json_decode($posterset['content'],true);
        foreach($posterdata as $k=>$v){
            if(isset($v['type']) && $v['type']=='head' && empty($v['radius']) ){
                $v['radius'] = 0;
            }
            $posterdata[$k] = $v;
        }
//        dump($posterdata);exit;
        $guize = $posterset['guize'];
        $poster_bg = $posterdata['poster_bg'];
        $poster_data = $posterdata['poster_data'];
        View::assign('type',$type);
        View::assign('poster_bg',$poster_bg);
//        dump($poster_data);exit;
        View::assign('poster_data',$poster_data);
        View::assign('guize',$guize);
        View::assign('posterid',$id);


        View::assign('platform',$platform);
        return View::fetch();
    }
    public function save(){
        $type = input('param.type') ? input('param.type') : $this->platform[0];
        $poster_bg = input('post.poster_bg');
        $poster_data = input('post.poster_data');
        $poster_data = htmlspecialchars_decode($poster_data);
        $data_index = ['poster_bg'=>$poster_bg,'poster_data'=>json_decode($poster_data)];
        $guize = input('post.guize');
        Db::name('admin_set_poster')->where('id',input('param.posterid'))->update(['content'=>json_encode($data_index),'guize'=>$guize]);
        if(input('post.clearhistory') == 1){
            $msg = '保存成功!';
        }else{
            $msg ='保存成功';
        }
        return json(['status'=>1,'msg'=>$msg,'url'=>(string)url('index').'/type/'.$type.'/id/'.input('param.posterid')]);
    }
}
